<template>
	<view class="setUp">
		<n-loading></n-loading>
		<view class="itemBox">
			<view class="eachItem">
				<view class="titBox" @tap="isShowGo">
					<view class="text">个人信息</view>
					<u-icon class="titArrowDown" :class="{titArrowDownEd:isShow}" :name="openS?'arrow-up':'arrow-down'" color="#999" size="20"></u-icon>
				</view>
				<view class="contBox" :style="[{height: isShow ? infoHeight : '0'}]" v-if="info">
					<view class="eachCont">
						<view class="text">头像</view>
						<u-avatar :src="info.avatar" size="104" @click="chooseAvatar"></u-avatar>
						<u-icon name="arrow-right" color="#999" size="20"></u-icon>
					</view>
					<view class="eachCont">
						<view class="text">昵称</view>
						<text @tap="fillPhone">{{info.nickname}}</text>
						<u-icon name="arrow-right" color="#999" size="20"></u-icon>
					</view>
				</view>
			</view>
			<view class="eachItem">
				<view class="titBox">
					<view class="text">隐私政策</view>
					<u-icon name="arrow-right" color="#999" size="20"></u-icon>
				</view>
			</view>
			<view class="eachItem">
				<view class="titBox" @tap="addressLink">
					<view class="text">地址管理</view>
					<u-icon name="arrow-right" color="#999" size="20"></u-icon>
				</view>
			</view>
			<view class="eachItem">
				<view class="titBox">
					<view class="text">联系客服</view>
					<u-icon name="arrow-right" color="#999" size="20"></u-icon>
				</view>
			</view>
			<view class="eachItem">
				<view class="titBox">
					<view class="text">当前版本</view>
					<text class="frText">{{info.current_version}}</text>
				</view>
			</view>
		</view>
		<view class="aloneBox" @tap="logoutGo">退出登录</view>
		<view class="aloneBox" @tap="cancellation">注销账户</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 信息
				info:null,
				openS:false,
				isShow:false,
				infoHeight:"auto", // 个人信息展开的高度
				avatar:'http://pic2.sc.chinaz.com/Files/pic/pic9/202002/hpic2119_s.jpg',//头像
			}
		},
		onShow() {
			this.getInfo();
		},
		created() {
			// 监听从裁剪页发布的事件，获得裁剪结果
			uni.$on('uAvatarCropper', path => {
				var _this = this;
				this.avatar = path;
				this.uploadImg('/public/fileupload',this.avatar).then(res =>{
					var data = JSON.parse(res.data)
					if(data.status == 200 ) {
						var newAvatar = data.data.file_url;
						this.apipost('/fun/member/update-setting',{avatar:newAvatar,nickname:this.info.nickname}).then(res =>{
							if(res.data.status == 200 ) {
								uni.showToast({
									icon: 'success',
									duration:1000,
									title: '设置成功',
									success: res => {
										setTimeout(function (){
											_this.getInfo();
										}, 1000);
									},
								});
							}
						})
					}
				})
			})
		},
		methods: {
			// 获取详情
			getInfo(){
				this.apiget('/fun/member/setting',{}).then(res =>{
					if(res.data.status == 200 ) {
						this.info = res.data.data;
					}
				})
			},
			// 展开个人信息
			isShowGo(){
				this.isShow = !this.isShow;
			},
			// 选择头像
			chooseAvatar() {
				// 此为uView的跳转方法，详见"文档-JS"部分，也可以用uni的uni.navigateTo
				this.$u.route({
					// 关于此路径，请见下方"注意事项"
					url: '/uview-ui/components/u-avatar-cropper/u-avatar-cropper',
					// 内部已设置以下默认参数值，可不传这些参数
					params: {
						// 输出图片宽度，高等于宽，单位px
						destWidth: 300,
						// 裁剪框宽度，高等于宽，单位px
						rectWidth: 200,
						// 输出的图片类型，如果'png'类型发现裁剪的图片太大，改成"jpg"即可
						fileType: 'jpg',
					}
				})
			},
			// 填写电话
			fillPhone(){
				uni.navigateTo({
					url: './fillPhone'
				});
			},
			addressLink(){
				uni.navigateTo({
					url:"/pages/mine/address"
				})
			},
			// 退出登录
			logoutGo(){
				this.apipost('/user/logout',{}).then(res =>{
					if(res.data.status == 200 ) {
						uni.removeStorage({
						    key: 'TOKEN',
						    success: function (res) {
						        uni.showToast({
						        	icon: 'success',
						        	duration:1000,
						        	title: '退出成功',
						        	success: res => {
						        		setTimeout(function (){
						        			uni.reLaunch({
						        			    url: '../index?barCur=0'
						        			});
						        		}, 1000);
						        	},
						        });
						    }
						});
					}
				})
			},
			// 注销
			cancellation(){
				this.apipost('/fun/member/cancellation',{}).then(res =>{
					if(res.data.status == 200 ) {
						uni.removeStorage({
						    key: 'TOKEN',
						    success: function (res) {
						        uni.showToast({
						        	icon: 'success',
						        	duration:1000,
						        	title: '注销成功',
						        	success: res => {
						        		setTimeout(function (){
						        			uni.reLaunch({
						        			    url: '../index?barCur=0'
						        			});
						        		}, 1000);
						        	},
						        });
						    }
						});
					}
				})
			},
		}
	}
</script>

<style lang="scss">
	.setUp{
		min-height: 100%;
		background: #F8F8FA;
	}
	.itemBox{
		padding: 0 32rpx;
		margin-top: 24rpx;
		background-color: #fff;
		
		.eachItem{
			.titBox{
				display: flex;
				align-items: center;
				padding: 28rpx 0;
				font-size: 28rpx;
				color: #333333;
				box-shadow: inset 0px -1rpx 0px #F0F0F0;
				
				.text{
					flex: 1;
				}
				u-icon{
					margin-right: 12rpx;
				}
				.titArrowDown{
					transition: all 0.3s;
				}
				.titArrowDownEd{
					transform: rotate(180deg);
					transform-origin: center center;
				}
				.frText{
					margin-right: 48rpx;
				}
			}
			.contBox{
				padding-left: 64rpx;
				overflow: hidden;
				transition: all 0.3s;
				
				.eachCont{
					display: flex;
					padding: 12rpx 0;
					min-height: 100rpx;
					align-items: center;
					box-shadow: inset 0px -1rpx 0px #F0F0F0;
					
					.text{
						flex: 1;
					}
					u-icon{
						margin-right: 12rpx;
						margin-left: 24rpx;
					}
				}
			}
		}
	}
	.aloneBox{
		padding: 26rpx 0;
		margin-top: 24rpx;
		font-size: 32rpx;
		line-height: 48rpx;
		text-align: center;
		color: #333333;
		background-color: #fff;
	}
</style>
